home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / netprog.zip / NETPROG.TAR / record / child1.c next >
C/C++ Source or Header  |  1989-12-17  |  832b  |  38 lines

  1. /*
  2.  * Exec a shell process, dup'ing the specified "fd" as its
  3.  * standard input, standard output and standard error.
  4.  */
  5.  
  6. #include    <stdio.h>
  7. #include    "systype.h"
  8.  
  9. exec_shell(fd, argv, envp)
  10. int    fd;        /* communication channel */
  11. char    **argv;
  12. char    **envp;
  13. {
  14.     char    *shell;
  15.     char    *getenv(), *rindex();
  16.  
  17.     close(0); close(1); close(2);
  18.     if (dup(fd) != 0 || dup(fd) != 1 || dup(fd) != 2)
  19.         err_sys("dup error");
  20.     close(fd);
  21.  
  22.     /*
  23.      * We must set up the pathname of the shell to be exec'ed,
  24.      * and its argv[0].
  25.      */
  26.  
  27.     if ( (shell = getenv("SHELL")) == NULL)    /* look at environment */
  28.         shell = "/bin/sh";    /* default */
  29.     if ( (argv[0] = rindex(shell, '/')) != NULL)
  30.         argv[0]++;        /* step past rightmost slash */
  31.     else
  32.         argv[0] = shell;    /* no slashes in pathname */
  33.  
  34.     execve(shell, argv, envp);
  35.     err_sys("execve error");
  36.         /* NOTREACHED */
  37. }
  38.